package com.is2.patchinglife.activity;




import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.is2.patchinglife.R;
import com.is2.patchinglife.manager.ActivityManager;
import com.is2.patchinglife.manager.LocalDataManager;
import com.is2.patchinglife.manager.RemoteDataManager;
import com.is2.patchinglife.wrapper.RankerChild;
import com.is2.patchinglife.wrapper.RankerList;

 
public class RankActivity extends Activity {
	private int nPerPage = 10;
	private int currentPage = 1;
	private String currentMode = "userRank"; //{userRank,trophyRank}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// draw base layout
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rank);

		//Add activity to manager
		ActivityManager.getInstance().addActivity("RankActivity", this);

		//request to Remote Server for data
		RemoteDataManager.getUserRank(1);
	
		//set Next Button Listener
		FrameLayout nextBtn = (FrameLayout) findViewById(R.id.rankNext);
		nextBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(RemoteDataManager.isOnline(RankActivity.this)){
					ActivityManager.executeButtonEffects();
					if(currentMode.compareTo("userRank")==0){
						RemoteDataManager.getUserRank(currentPage +1);
					}else{
						RemoteDataManager.getUserRank(currentPage+1,"trophy");
					}
				}
			}
		});
		
		//set Next Button Listener
		FrameLayout prevBtn = (FrameLayout) findViewById(R.id.rankPrev);
		prevBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ActivityManager.executeButtonEffects();
				if(RemoteDataManager.isOnline(RankActivity.this)){
					if(currentMode.compareTo("userRank")==0){
						RemoteDataManager.getUserRank(currentPage -1);
					}else{
						RemoteDataManager.getUserRank(currentPage-1,"trophy");
					}
				}
			}
		});
		

		//set userRank Button Listener
		FrameLayout userRankBtn = (FrameLayout) findViewById(R.id.rankUserBtn);
		userRankBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ActivityManager.executeButtonEffects();
				if(RemoteDataManager.isOnline(RankActivity.this)){
					RemoteDataManager.getUserRank(1);
					currentMode="userRank";
				}
			}
		});
		

		//set trophyRank Button Listener
		FrameLayout trophyRankBtn = (FrameLayout) findViewById(R.id.rankTrophyBtn);
		trophyRankBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ActivityManager.executeButtonEffects();
				if(RemoteDataManager.isOnline(RankActivity.this)){
					RemoteDataManager.getUserRank(1,"trophy");
					currentMode="trophyRank";
				}
			}
		});
		
		
				
	}
    @Override
    public void onDestroy(){
        super.onDestroy();
        
		String[] aList = {"RankActivity"};
	 	ActivityManager.getInstance().closeActivities(aList,false);
	 	
    }
    
    
    public void drawRankList(String _JSON,String _currentMode){
    	
    	//get utility service to create position element
    	LinearLayout container = (LinearLayout) findViewById(R.id.rankerList);
    	    	
		LayoutInflater infalInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View listElement;

		// parse server response
		Gson parser = new Gson();
		RankerList rList = null;
		try{
			 rList = parser.fromJson(_JSON, new TypeToken<RankerList>(){}.getType());
		}
		catch(Exception e){
			//CATCH PARSING
			return;
		}
		
		// update rank type
		String units = "";
		if(currentMode.compareTo("userRank")==0){
			units = "points";
		}else{
			units = "trophies";
		}
		
		// remove old rank box (not the user box
		int oldListCount= container.getChildCount();
		
		for(int i=1;i<oldListCount;i++){
			container.removeViewAt(1);
		}
		
		
		//Add User Position to the page
		FrameLayout userPosition = (FrameLayout) findViewById(R.id.rankUserPosition);
		
		TextView position = (TextView) userPosition.findViewById(R.id.rankerPosition);
		position.setText(String.valueOf(rList.getUserRank().getRank()));
		
		TextView username = (TextView) userPosition.findViewById(R.id.rankerName);
		username.setText(LocalDataManager.getUser());
		
		TextView points = (TextView) userPosition.findViewById(R.id.rankerPoints);
		points.setText(String.valueOf(rList.getUserRank().getPoints())+" "+units);
				
		int i=1;
		int page = rList.getPage()-1;
		if(rList.getRankList() != null){
			//insert 
			for(RankerChild rChild:rList.getRankList()){
				
				listElement = infalInflater.inflate(R.layout.rank_child,null);
	
				position = (TextView) listElement.findViewById(R.id.rankerPosition);
				position.setText(String.valueOf(rChild.getRank()));
				
				username = (TextView) listElement.findViewById(R.id.rankerName);
				username.setText(rChild.getUser());
				
				points = (TextView) listElement.findViewById(R.id.rankerPoints);
				
				
				points.setText(String.valueOf(rChild.getPoints())+" "+units);
				
				
				container.addView(listElement);
				
				i++;
			}
			// fill void position if there are any
			for(; i<= nPerPage;i++){
				
				listElement = infalInflater.inflate(R.layout.rank_child,null);
	
				position = (TextView) listElement.findViewById(R.id.rankerPosition);
				position.setText(String.valueOf(i+(nPerPage*page)));
				
				username = (TextView) listElement.findViewById(R.id.rankerName);
				username.setText("Uknown");
				
				points = (TextView) listElement.findViewById(R.id.rankerPoints);
				points.setText("//");
				
				
				//container.addView(listElement);
				
			}
			
			// hide progress bar
			ProgressBar a = (ProgressBar) findViewById(R.id.rankProgressBar);
			a.setVisibility(View.GONE);
			container.setVisibility(View.VISIBLE);
			
			
			//Show Or Hide Previous Page Button
			FrameLayout prevBtn = (FrameLayout) findViewById(R.id.rankPrev);
			if(rList.getPage()> 1){
				prevBtn.setVisibility(View.VISIBLE);
			}else{
				prevBtn.setVisibility(View.INVISIBLE);
			}
			FrameLayout nextBtn = (FrameLayout) findViewById(R.id.rankNext);
			
			//Show Or Hide next Page Button
			if(rList.getPage()< rList.getTotalPage()-1){
				nextBtn.setVisibility(View.VISIBLE);
			}else{
				nextBtn.setVisibility(View.INVISIBLE);
			}
			// Update current page
			currentPage = rList.getPage();
			
		}else{
			// Show alert
			 AlertDialog.Builder builder=new AlertDialog.Builder(this);
				builder.setTitle("Alert!");
				builder.setMessage("There are no more pages.");
				builder.setCancelable(true);
				
				builder.setPositiveButton("OK",null);

				AlertDialog alert=builder.create();
				alert.show();
			
		}
    }
    
    
}
